<?php
/**
 * @description : to do success process when csl page has completed process.
 * @author      : stephen.mo <stephen.mo@achievo.com>
 * @date        : Mar 11,2011
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set("display_errors","on");

$token = (isset($_REQUEST['token'])) ? $_REQUEST['token'] : NULL;
$oauth_callback = (isset($_REQUEST['oauth_callback'])) ? $_REQUEST['oauth_callback'] : NULL;
$ntype = (isset($_REQUEST['ntype'])) ? $_REQUEST['ntype'] : NULL;
$tvbauth = (isset($_REQUEST['tvbauth'])) ? $_REQUEST['tvbauth']: NULL;
$lang = (isset($_REQUEST['lang']) && !empty($_REQUEST['lang'])) ? $_REQUEST['lang'] : NULL;
$language = (isset($_REQUEST['language'])) ? $_REQUEST['language'] : NULL;
$lang = (empty($lang) && !empty($language) &&  isset(OAuthModelConfig::$mapping_third_parties_lang[$language])) ? OAuthModelConfig::$mapping_third_parties_lang[$language] : OAuthConfig::DEFAULT_SUPPORT_LANG;
$base_server_path  = OAuthConfig::OAuthDomainUrl($_SERVER);
$message_url       = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;

//check parameter not allow null.
if (empty($oauth_callback) || empty($token) || empty($tvbauth)) {
    $retry = (!empty($oauth_callback)) ? $oauth_callback : '#';
    $other_msg = array('call_back'=>urlencode($retry));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1035,1037),$other_msg);
    exit;
}


//to analyze a url and get some necessary parameter.
$param_vars = array();
if (!empty($oauth_callback)) {
    $query_param_vars  = parse_url($oauth_callback);
    if (isset($query_param_vars['query'])) {
        parse_str($query_param_vars['query'],$param_vars);
    }
}
$call_back = (isset($param_vars['call_back'])) ? $param_vars['call_back'] : $oauth_callback;
$network_type = (isset($param_vars['ntype'])) ? $param_vars['ntype'] : NULL;

//call success back API.
$rec = OAuthUtils::cslPaymentSuccessBack($tvbauth,$token,$ntype);
if (empty($rec)) {
     $other_msg = array('call_back'=>urlencode($oauth_callback));
    OAuthUtils::toMessageUrl($message_url,OAuthModelConfig::M_PAGE_OF_ERROR,OAuthModelConfig::DEFAULT_CONST_FALIURE,$lang,array(1041),$other_msg);
    exit;
}

//redirect to a static html page.
$message_url = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL.'?act=request&flag='.OAuthModelConfig::DEFAULT_CONST_SUCCESS.'&lang='.$lang;
OAuthUtils::redirectTo($message_url);
exit;

?>
